package com.wk1un2.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.wk1un2.util.BaseServlet;
import com.wk1un2.util.Result;
//上传图片控制器
@WebServlet("/UploadpictServlet")
public class UploadpictServlet extends BaseServlet {

	// ͼƬ�ϴ�
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Result res = null;
		Gson g = new Gson();
		PrintWriter out = response.getWriter();
		// 1.����SmartUpload
		SmartUpload smart = new SmartUpload();
		// 2.�����ַ���
		smart.setCharset("utf-8");
		// 3.��ʼ��
		smart.initialize(getServletConfig(), request, response);
		try { // 4.�ϴ��ļ�
			smart.upload();
			// 5.�����ļ���ָ��λ��(saveֻ�ṩ·����ʹ��ԭ�ļ����ϴ��ļ�)
			smart.save("/Backstage/upload");
			// ��ȡ�ļ���
			String filename = smart.getFiles().getFile(0).getFileName();
			res = new Result(true, 200, "�ϴ��ɹ�", 1, filename);
			out.print(g.toJson(res));
		} catch (SmartUploadException e) {
			res = new Result(false, 200, "�ϴ�ʧ��", 0, null);
			out.print(g.toJson(res));
			e.printStackTrace();
		}
		out.flush();
		out.close();
	}

}